package blocks;

import items.ItemBlock;
import main.Map;

import org.newdawn.slick.Color;
import org.newdawn.slick.GameContainer;
import org.newdawn.slick.Graphics;
import org.newdawn.slick.Image;
import org.newdawn.slick.SlickException;

import entities.WorldEntity;

public abstract class Block extends WorldEntity {
	private Image texture;
	private String type;
	private int size;
	private int hitDrawCount;
	private int hitDrawCountMax;
	
	public Block(int x, int y, int width, int height) {
		super(x, y, width, height);
		
		hitDrawCountMax = 20;
		hitDrawCount = hitDrawCountMax;
		setHealth(2);
		size = 40;
	}

	@Override
	public void update(GameContainer gc) {
		
	}

	@Override
	public void draw(GameContainer gc, Graphics g) {
		if(texture != null) {
			g.drawImage(texture, getX(), getY());
			g.setColor(Color.white);
			//g.drawString(Integer.toString(getHealth()), getX(), getY());
			
			if(hitDrawCount < hitDrawCountMax) {
				g.setColor(Color.gray);
				g.fillRect(getX(), getY(), getWidth(), getHeight());
				hitDrawCount++;
			}
		}
	}
	
	public void takeDamage(int damage) {
		setHealth(getHealth() - damage);
		hitDrawCount = 0;
	}
	
	public void checkHealth() throws SlickException {
		if(getHealth() <= 0) {
			if(type.equals("grass"))
				type = "earth";
			
			ItemBlock item = new ItemBlock(Map.getItemIndexCounter(), getX(), getY(), size, size, type);
			Map.addItemByIndex(Map.getItemIndexCounter(), item);
			Map.setItemIndexCounter(Map.getItemIndexCounter() + 1);
			Map.removeBlock(getX(), getY());
		}
	}

	public Image getTexture() {
		return texture;
	}

	public void setTexture(String path) throws SlickException {
		texture = new Image(path);
	}

	public String getType() {
		return type;
	}

	public void setType(String type) {
		this.type = type;
	}

}
